package com.hanrui.android.net.presenter.message.myvote;

import android.content.Context;
import android.util.Log;

import com.hanrui.android.activity.LoginActivity;
import com.hanrui.android.library.base.BaseBean;
import com.hanrui.android.library.base.BasePresenter;
import com.hanrui.android.constant.Constant;
import com.hanrui.android.constant.LoadType;
import com.hanrui.android.model.AlreadyVoteListBean;
import com.hanrui.android.model.NotYetVoteListBean;
import com.hanrui.android.library.net.ApiService;
import com.hanrui.android.library.net.RetrofitManager;
import com.hanrui.android.library.net.RxSchedulers;
import com.hanrui.android.net.contract.message.myvote.MyVoteListContract;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 * 表决列表
 */

public class MyVoteListPresenter extends BasePresenter<MyVoteListContract.View> implements MyVoteListContract.Presenter {

    private boolean mIsRefresh;

    @Inject
    public MyVoteListPresenter(){this.mIsRefresh = true;}

    @Override
    public void notYetList(final Context context,String type) {

        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .notYetList(type)
                .compose(RxSchedulers.<BaseBean<NotYetVoteListBean>>applySchedulers())
                .compose(mView.<BaseBean<NotYetVoteListBean>>bindToLife())
                .subscribe(new Consumer<BaseBean<NotYetVoteListBean>>() {
                    @Override
                    public void accept(BaseBean<NotYetVoteListBean> dataResponse) throws Exception {
                        Log.e(Constant.tag,dataResponse.getCode()+"");
                        if (dataResponse.getCode() == 1){
                            int loadType = mIsRefresh? LoadType.TYPE_REFRESH_SUCCESS :LoadType.TYPE_LOAD_MORE_SUCCESS;
                            mView.notYetList(dataResponse.getData().getNoVoteList(),loadType);
                        }else if (dataResponse.getCode() == 201){
                            LoginActivity.start(context);
                            mView.showFaild("会话过期请重新登录");
                        }else {
                            int loadType = mIsRefresh? LoadType.TYPE_REFRESH_ERROR : LoadType.TYPE_LOAD_MORE_ERROR;
                            mView.notYetList(null,loadType);
                            mView.showFaild(String.valueOf(dataResponse.getMessage()));
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        int loadType = mIsRefresh? LoadType.TYPE_REFRESH_ERROR : LoadType.TYPE_LOAD_MORE_ERROR;
                        mView.notYetList(null,loadType);
                        mView.showFaild(throwable.getMessage());
                    }
                });

    }

    @Override
    public void alreadyList(final Context context,String type) {

        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .alreadyList(type)
                .compose(RxSchedulers.<BaseBean<AlreadyVoteListBean>>applySchedulers())
                .compose(mView.<BaseBean<AlreadyVoteListBean>>bindToLife())
                .subscribe(new Consumer<BaseBean<AlreadyVoteListBean>>() {
                    @Override
                    public void accept(BaseBean<AlreadyVoteListBean> dataResponse) throws Exception {
                        Log.e(Constant.tag,dataResponse.getCode()+"");
                        if (dataResponse.getCode() == 1){
                            int loadType = mIsRefresh? LoadType.TYPE_REFRESH_SUCCESS :LoadType.TYPE_LOAD_MORE_SUCCESS;
                            mView.alreadyList(dataResponse.getData().getNoVoteList(),loadType);
                        }else if (dataResponse.getCode() == 201){
                            LoginActivity.start(context);
                            mView.showFaild("会话过期请重新登录");
                        }else {
                            int loadType = mIsRefresh? LoadType.TYPE_REFRESH_ERROR : LoadType.TYPE_LOAD_MORE_ERROR;
                            mView.alreadyList(null,loadType);
                            mView.showFaild(String.valueOf(dataResponse.getMessage()));
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        int loadType = mIsRefresh? LoadType.TYPE_REFRESH_ERROR : LoadType.TYPE_LOAD_MORE_ERROR;
                        mView.alreadyList(null,loadType);
                        mView.showFaild(throwable.getMessage());
                    }
                });

    }

    @Override
    public void refresh(Context context,String type) {
        mIsRefresh = true;
        if (type.equals("1")) {
            notYetList(context,type);
        }else {
            alreadyList(context,type);
        }
    }
}
